<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>
    <!-- /导航栏 -->

    <!-- 加载中 loading -->
    <van-loading
      v-if="loading"
      class="article-loading"
    />
    <!-- /加载中 loading -->
    <div
      class="error"
      v-if="is404"
    >
      <p>404页面</p>
    </div>
    <!-- 文章详情 -->
    <div
      v-else
      class="detail"
    >
      <h3 class="title">{{article.title}}</h3>
      <div class="author">
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="fill"
          :src="article.aut_photo"
        />
        <div class="text">
          <p class="name">{{article.aut_name}}</p>
          <p class="time">{{article.pubdate | timeFilter}}</p>
        </div>
        <van-button
          round
          size="small"
          type="info"
          @click="hSwitchFollow"
        >{{article.is_followed?'已关注':'+ 关注'}}</van-button>
      </div>
      <div class="content">
        <div v-html="article.content"></div>
      </div>
      <van-divider>END</van-divider>
      <div class="zan">
        <van-button
          @click="hSwitchLike"
          round
          size="small"
          hairline
          type="primary"
          plain
          :icon="article.attitude===1?'good-job':'good-job-o'"
        >{{article.attitude===1?'已点赞':'点赞'}}</van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          @click="hSwitchDisLike"
          round
          size="small"
          hairline
          type="danger"
          plain
          icon="delete"
        >{{article.attitude===0?'取消不喜欢':'不喜欢'}}</van-button>
      </div>
    </div>
    <!-- /文章详情 -->
    <comment :articleId="$route.params.id"></comment>
  </div>
</template>

<script>
import { getArticleList, likeArticle, concleLike, disLike, concleDisLike } from '../../api/article'
import { followAuthor, delAuthor } from '../../api/user'
import comment from './comment.vue'
export default {
  name: 'ArticleIndex',
  data () {
    return {
      is404: false,
      loading: true, // 控制加载中的 loading 状态
      article: {}
    }
  },
  created () {
    // 获取当前文章数据
    this.loadArticle(this.$route.params.id)
  },
  components: {
    comment
  },
  methods: {
    async hSwitchFollow () {
      this.article.is_followed = !this.article.is_followed
      if (this.article.is_followed) {
        try {
          const res = await followAuthor(this.article.aut_id)
          console.log(res)
          this.$toast.success('关注成功')
        } catch (err) {
          console.log(err)
          this.$toast.fail('关注失败')
        }
      } else {
        try {
          const res = await delAuthor(this.article.aut_id)
          console.log('qx', res)
          this.$toast.success('成功取关')
        } catch (err) {
          console.log(err)
          this.$toast.fail('取关失败')
        }
      }
    },
    async loadArticle (id) {
      try {
        this.loading = false

        this.is404 = false
        const res = await getArticleList(id)
        console.log('art', res)
        this.article = res.data.data
        this.$toast.success('文章加载成功')
      } catch (err) {
        console.log(err)
        if (err.response.status === 404) {
          this.is404 = true
        }
      }
    },
    hSwitchLike () {
      if (this.article.attitude === 1) {
        this.conLikeArt(this.article.art_id)
      } else {
        this.likeArt(this.article.art_id)
      }
    },
    async likeArt (id) {
      try {
        const res = await likeArticle(id)
        console.log(res)
        this.article.attitude = 1
        this.$toast.success('点赞成功')
      } catch (err) {
        console.log(err)
        this.$toast.fail('点赞失败')
      }
    },
    async conLikeArt (id) {
      try {
        const res = await concleLike(id)
        console.log(res)
        this.article.attitude = -1
        this.$toast.success('取消点赞')
      } catch (err) {
        console.log(err)
        this.$toast.fail('取消失败')
      }
    },
    hSwitchDisLike () {
      if (this.article.attitude === 0) {
        this.conDisLikeArt(this.article.art_id)
      } else {
        this.disLikeArt(this.article.art_id)
      }
    },
    async disLikeArt (id) {
      try {
        const res = await disLike(id)
        console.log(res)
        this.article.attitude = 0
        this.$toast.success('不喜欢成功')
      } catch (err) {
        console.log(err)
        this.$toast.fail('失败')
      }
    },
    async conDisLikeArt (id) {
      try {
        const res = await concleDisLike(id)
        console.log(res)
        this.article.attitude = -1
        this.$toast.success('成功')
      } catch (err) {
        console.log(err)
        this.$toast.fail('失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.article-container {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error {
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan {
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size: 14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img {
      max-width: 100%;
      background: #f9f9f9;
    }
  }
}
</style>
